home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d998.lha / TeXPrt / source / TeXPrt.c < prev    next >
C/C++ Source or Header  |  1994-04-05  |  20KB  |  457 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Filename : TeXPrt.c                                                    *
  4.  *                                                                         *
  5.  *  Description : A front-end for DVI printer driver.                      *
  6.  *                                                                         *
  7.  *  Return Codes :  0 - Program finished without errors.                   *
  8.  *                  1 - Error abort.                                       *
  9.  *                                                                         *
  10.  ***************************************************************************
  11.  *                                                                         *
  12.  *                         Modification History                            *
  13.  *                                                                         *
  14.  *  Date      Author       Comments                                        *
  15.  * ----------------------------------------------------------------------  *
  16.  *  3.3.92    R.Bödi       Created.                                        *
  17.  *  5.3.92    R.Bödi       Added Printout-requester and config file.       *
  18.  * 29.6.92    R.Bödi       Added 'No of Copies' string gadget.             *
  19.  *                         Changed log shell.                              *
  20.  * 24.9.92    R.Bödi       Added ability to use the DVILJ2P printer driver *
  21.  *                         as an alternative to the PasTeX DVIPrint.       *
  22.  *                         Disadvantage : No metafont call if a font is    *
  23.  *                                        not available.                   *
  24.  * 25.9.92    R.Bödi       Added AREXX-Port.                               *
  25.  * 24.10.92   R.Bödi       Few changes for SAS/C 6.0                       *
  26.  * 25.10.92   R.Bödi       Now fetches the actual filename of the DVI file *
  27.  *                         from the ShowDVI previewer if possible. This    *
  28.  *                         needs the ARexx script file TeXPrtPrint.tpr to  *
  29.  *                         work.                                           *
  30.  * 28.10.92   R.Bödi       Added NOREQ option to the AREXX print command   *
  31.  *                         which suppresses the confirmation requester.    *
  32.  * 22.5.93    R.Bödi       Now multiple TeX printer driver are supported   *
  33.  *                         via driver description files (ASCII).           *
  34.  * 19.6.93    R.Bödi       Added Boopsi gadget for calling file requester. *
  35.  *                         Thanks to Jan van den Baard for supporting it.  *
  36.  *                         Added AppIcon and AppWindow.                    *
  37.  * 2.11.93    R.Bödi       Changed structure TeXDriver, see TeXPrt.h       *
  38.  * 12.2.94    R.Bödi       Turned TeXPrt into a MUI application.           *
  39.  *                                                                         *
  40.  ***************************************************************************
  41.  *                                                                         *
  42.  * Copyright © 1992-1994 Richard Bödi,  All rights reserved.               *
  43.  *                                                                         *
  44.  ***************************************************************************/
  45.  
  46. /*----------------------------- INCLUDES ----------------------------------*/
  47.  
  48. #include "TeXPrt.h"
  49. #include "TexPrt_protos.h"
  50. #include "TexPrt_globals.h"
  51.  
  52. /*-------------------------- GLOBAL DATA ----------------------------------*/
  53.  
  54. char  AppName[]          = "TeXPrt";
  55. char  Version[]          = "$VER: TeXPrt"VERSION""__AMIGADATE__;
  56. char  Title[]            = "TeXPrint";
  57. char  TempFileName[]     = "t:TeXPrint.tmp";
  58. char  DefaultShell[]     = "newshell CON:0/0/640/150/TeXPrint-Output";
  59. char  DriverDrawer[]     = "TeXDrivers";
  60. char  DriverLogo[]       = "TeXPrt printer driver spec file";
  61. char  TeXPrintEnv[]      = "TeXPrint";
  62. char  RexxPrintMacro[]   = "run sys:Rexxc/rx TexPrtPrint.tpr";
  63. char  RexxStartupMacro[] = "run sys:Rexxc/rx TexPrtInit.tpr";
  64.  
  65. struct Args             Args;
  66. struct AvailDrivers     Drivers;
  67. struct TeXDriver        TeXDriver;
  68. struct RawSettings      RawSettings;
  69. struct ParsedSettings   ParsedSettings;
  70. struct DefaultSettings  DefaultSettings;
  71. struct MUIPointers      MUIPointers;
  72. struct StructPointers   StructPointers;
  73.  
  74. USHORT               ActualDriver;
  75. ULONG                IDCMPFlag = 0;
  76.  
  77. struct MsgPort      *AppMsgPort = NULL;
  78. struct DiskObject   *AppIconObj = NULL;
  79. struct AppIcon      *AppIcon = NULL;
  80. struct Library      *MUIMasterBase = NULL;
  81. struct NewMenu      *Menu = NULL;
  82.  
  83. struct EasyStruct
  84.    ConfirmReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"Confirm Printout",
  85.                   (UBYTE *)"\n     Start Printing ?     \n\n Printer : %s \n \n Print %s \n",
  86.                   (UBYTE *)"Confirm|Cancel" },
  87.  
  88.      AboutReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"About",
  89.                   (UBYTE *)"\n   %s \n\n A front end for the DVI printer driver \n\n\
  90.   (c) 1993-1994 by Richard Bödi\n\n\
  91.   Mathematisches Institut\n\
  92.   Universität Tübingen\n\
  93.   Auf der Morgenstelle 10\n\
  94.   72076 Tübingen\n\
  95.   Germany\n\n\
  96.  e-mail :\n\
  97.   mmisa01@mailserv.zdv.uni-tuebingen.de\n",
  98.                   (UBYTE *)"OK" },
  99.  
  100.    AppIconReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"About",
  101.                   (UBYTE *)"\n  This is the AppIcon of the TeXPrt program.\
  102.   \n       To select a DVI-File for printing, \n\
  103.        simply drag its icon over this one. \n",
  104.                   (UBYTE *)"Fine|Quit TeXPrt" },
  105.  
  106.  LoadErrorReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"Load Error",
  107.                   (UBYTE *)"\n Error in TeXDriver specification file \n %s\n Last line read: %ld\n",
  108.                   (UBYTE *)"Accept" },
  109.  
  110.  SaveErrorReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"Save Error",
  111.                   (UBYTE *)"\n Couldn't save TeXDriver specification file \n\n   %s \n",
  112.                   (UBYTE *)"Accept" },
  113.  
  114.  NoDVIFileReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"No DVI file specified",
  115.                   (UBYTE *)"\n Please select a DVI file to print.\n", (UBYTE *)"of course" },
  116.  
  117.       QuitReq = { sizeof (struct EasyStruct), 0, (UBYTE *)"Quit Program",
  118.                   (UBYTE *)"\n Really quit the program ?\n", (UBYTE *)"Yes|No" };
  119.  
  120.  
  121. /*------------------------------- CODE ------------------------------------*/
  122.  
  123. main (int argc, char *argv[])
  124.  
  125. {
  126. int               Error = 1;
  127. USHORT            Line;
  128. long              Signals = 0;
  129. char              RexxPortName[NAMELENGTH];
  130. BPTR              OldDirectory, OwnDirectory;
  131. struct MsgPort   *PortFound;
  132.  
  133.  
  134. OwnDirectory = GetProgramDir ();
  135. OldDirectory = CurrentDir (OwnDirectory);
  136.  
  137. stpcpy (RexxPortName, AppName);
  138. strupr (RexxPortName);
  139. strcat (RexxPortName, ".1");
  140.  
  141. Forbid ();
  142. PortFound = FindPort (RexxPortName);
  143. Permit ();
  144.  
  145. if (!PortFound)
  146.    {
  147.    stpcpy (Args.ArgStr[KW_SHELL], DefaultShell);
  148.  
  149.    if (argc == 0)
  150.       ParseWB ((struct WBStartup *)argv);
  151.    if (argc > 1)
  152.       ParseShell (argc, argv);
  153.  
  154.    if (OpenLibs ())
  155.       printf ("Error opening library.\n");
  156.    else
  157.       {
  158.       Drivers.NoOfDrivers = ScanDrivers (&Drivers);
  159.  
  160.       if (Drivers.NoOfDrivers <= 0)
  161.          {
  162.          if (Drivers.NoOfDrivers == 0)
  163.             printf ("No TeX printer drivers found in drawer %s.\n", DriverDrawer);
  164.          else
  165.             printf ("DOS error or no memory for pattern matching. \n");
  166.          }
  167.       else
  168.          {
  169.          if (!(Menu = BuildMenuStructure (&Drivers, &ActualDriver)))
  170.             printf ("Couldn't build up menu.\n");
  171.          else
  172.             {
  173.             if (!BuildMUIApp (&MUIPointers, Menu))
  174.                printf ("Couldn't create MUI application.\n");
  175.             else
  176.                {
  177.                StructPointers.AvailDrivers     = &Drivers;
  178.                StructPointers.TeXDriver        = &TeXDriver;
  179.                StructPointers.RawSettings      = &RawSettings;
  180.                StructPointers.ParsedSettings   = &ParsedSettings;
  181.                StructPointers.DefaultSettings  = &DefaultSettings;
  182.                StructPointers.DriverNumber     = ActualDriver;
  183.                StructPointers.MUIPointers      = &MUIPointers;
  184.  
  185.                if (Line = LoadDriverSpec (&StructPointers, ActualDriver))
  186.                   EasyRequest (MUIPointers.MainWindow, &LoadErrorReq, &IDCMPFlag,
  187.                                Drivers.DriverFiles + ActualDriver * NAMELENGTH, Line);
  188.                else
  189.                   UpdateGUI (&MUIPointers, &DefaultSettings, &ParsedSettings);
  190.  
  191.                SetAttrs (MUIPointers.STR_DVIFile, MUIA_String_Contents, Args.ArgStr[KW_FILE], TAG_DONE);
  192.  
  193.                Forbid ();
  194.                PortFound = FindPort (RexxPortName);
  195.                Permit ();
  196.                if (PortFound) StructPointers.ARexxAvailable = TRUE;
  197.                else           StructPointers.ARexxAvailable = FALSE;
  198.  
  199.                if (Args.ArgStr[KW_APPICON][0])
  200.                   if (BuildAppIcon (&AppMsgPort, &AppIconObj, &AppIcon, Args.ArgStr[KW_APPICON]))
  201.                      Signals = 1L<<AppMsgPort->mp_SigBit;
  202.  
  203.                ProcessIDCMP (&StructPointers, Signals);
  204.                Error = 0;
  205.                }
  206.             }
  207.          }
  208.       }
  209.    }
  210. else
  211.    system (RexxStartupMacro);
  212.  
  213. CurrentDir (OldDirectory);
  214. Cleanup ();
  215. return (0);
  216. }
  217.  
  218.  
  219. /***************************************************************************
  220.  *                                                                         *
  221.  *  Function name : CallPrinterDriver                                      *
  222.  *                                                                         *
  223.  *  Description : Creates the parameter list for the printer driver and    *
  224.  *                calls the appropriate printer driver.                    *
  225.  *                                                                         *
  226.  ***************************************************************************
  227.  *                                                                         *
  228.  *  Synopsis : CallPrinterDriver (Pointers, NoReq)                         *
  229.  *                                                                         *
  230.  *  Parameters :                                                           *
  231.  *    (struct Structures *) Pointers:                                      *
  232.  *       A structure consisting of APTRs to various data structures.       *
  233.  *    (BOOL) NoReq :                                                       *
  234.  *       TRUE, if a confirmation requester should appear,                  *
  235.  *      FALSE, otherwise                                                   *
  236.  *                                                                         *
  237.  *  Return value : None                                                    *
  238.  *                                                                         *
  239.  ***************************************************************************/
  240.  
  241. void  CallPrinterDriver (struct StructPointers *Pointers, BOOL NoReq)
  242.  
  243. {
  244. int      OptionCount, Cnt;
  245. ULONG    GetInt, From, To, Copies;
  246. char    *DVIFile;
  247. char     PrinterName[NAMELENGTH], Pages[NAMELENGTH],
  248.          Command[CMDSTRLENGTH], TempBuffer[CMDLENGTH], Console[CMDSTRLENGTH];
  249. BOOL     Print;
  250. FILE    *TmpFp;
  251.  
  252.  
  253. set (Pointers->MUIPointers->app, MUIA_Application_Sleep, TRUE);
  254.  
  255. GetAttr (MUIA_String_Integer, Pointers->MUIPointers->STR_From, &From);
  256. GetAttr (MUIA_String_Integer, Pointers->MUIPointers->STR_To, &To);
  257.  
  258. if (GetVar ("DVIPrint", PrinterName, sizeof (PrinterName), GVF_GLOBAL_ONLY) == -1)
  259.    stpcpy (PrinterName, "printer=generic");
  260.  
  261. GetAttr (MUIA_Radio_Active, Pointers->MUIPointers->RAD_FromTo, &GetInt);
  262. if (GetInt == DOCUMENT)
  263.    stpcpy (Pages, "whole document");
  264. else
  265.    if (From == To) sprintf (Pages, "page %d", From);
  266.    else            sprintf (Pages, "pages %d to %d", From, To);
  267.  
  268. GetAttr (MUIA_Radio_Active, Pointers->MUIPointers->RAD_OddEven, &GetInt);
  269. if (GetInt == ODD)  strcat (Pages, " (odd pages)");
  270. if (GetInt == EVEN) strcat (Pages, " (even pages)");
  271.  
  272. if (NoReq) Print = TRUE;
  273. else       Print = EasyRequest (Pointers->MUIPointers->MainWindow, &ConfirmReq, &IDCMPFlag, &PrinterName[8], Pages);
  274.  
  275. GetAttr (MUIA_String_Contents, Pointers->MUIPointers->STR_DVIFile, &GetInt);
  276. DVIFile = (char *)GetInt;
  277.  
  278. if ((strlen (DVIFile) == 0) && Print)
  279.    {
  280.    EasyRequest (Pointers->MUIPointers->MainWindow, &NoDVIFileReq, &IDCMPFlag, NULL);
  281.    Print = FALSE;
  282.    }
  283.  
  284. if (Print)
  285.    {
  286.    stpcpy (Command, Pointers->TeXDriver->ProgramNode);
  287.    strcat (Command, Pointers->ParsedSettings->Misc);
  288.    GetAttr (MUIA_Radio_Active, Pointers->MUIPointers->RAD_FromTo, &GetInt);
  289.  
  290.    if (GetInt == FROMTO)
  291.       {
  292.       sprintf (TempBuffer, " %s%d", Pointers->ParsedSettings->From, From);
  293.       strcat (Command, TempBuffer);
  294.       if (strcmp (Pointers->ParsedSettings->To, "  "))
  295.          sprintf (TempBuffer, " %s%d", Pointers->ParsedSettings->To, To);
  296.       else
  297.          sprintf (TempBuffer, " %s%d", Pointers->ParsedSettings->NoOfPages, To - From + 1);
  298.       strcat (Command, TempBuffer);
  299.       }
  300.  
  301.    if (strcmp(Pointers->ParsedSettings->Copies, "  "))
  302.       {
  303.       GetAttr (MUIA_String_Integer, Pointers->MUIPointers->STR_Copies, &Copies);
  304.       sprintf (TempBuffer, " %s%d", Pointers->ParsedSettings->Copies, Copies);
  305.       strcat (Command, TempBuffer);
  306.       }
  307.  
  308.    GetAttr (MUIA_Radio_Active, Pointers->MUIPointers->RAD_OddEven, &GetInt);
  309.    switch (GetInt)
  310.       {
  311.       case ALL:
  312.          sprintf (TempBuffer, " %s", Pointers->ParsedSettings->AllPages);
  313.          break;
  314.       case ODD:
  315.          sprintf (TempBuffer, " %s", Pointers->ParsedSettings->OddPages);
  316.          break;
  317.       case EVEN:
  318.          sprintf (TempBuffer, " %s", Pointers->ParsedSettings->EvenPages);
  319.          break;
  320.       }
  321.    strcat (Command, TempBuffer);
  322.  
  323.    GetAttr (MUIA_Radio_Active, Pointers->MUIPointers->RAD_Orientation, &GetInt);
  324.    if (GetInt == PORTRAIT)
  325.       sprintf (TempBuffer, " %s", Pointers->ParsedSettings->Portrait);
  326.    else
  327.       sprintf (TempBuffer, " %s", Pointers->ParsedSettings->LandScape);
  328.    strcat (Command, TempBuffer);
  329.  
  330.    if (strcmp(Pointers->ParsedSettings->HOffset, "  "))
  331.       {
  332.       GetAttr (MUIA_String_Integer, Pointers->MUIPointers->STR_X, &GetInt);
  333.       sprintf (TempBuffer, " %s%d%s", Pointers->ParsedSettings->HOffset,
  334.                                       GetInt + Pointers->DefaultSettings->AdjHOffset,
  335.                                       Pointers->ParsedSettings->HOffsetUnit);
  336.       strcat (Command, TempBuffer);
  337.       }
  338.  
  339.    if (strcmp(Pointers->ParsedSettings->VOffset, "  "))
  340.       {
  341.       GetAttr (MUIA_String_Integer, Pointers->MUIPointers->STR_Y, &GetInt);
  342.       sprintf (TempBuffer, " %s%d%s", Pointers->ParsedSettings->VOffset,
  343.                                       GetInt + Pointers->DefaultSettings->AdjVOffset,
  344.                                       Pointers->ParsedSettings->VOffsetUnit);
  345.       strcat (Command, TempBuffer);
  346.       }
  347.  
  348.    for (OptionCount = 0; OptionCount < OPTIONS; OptionCount++)
  349.       {
  350.       GetAttr (MUIA_Selected, Pointers->MUIPointers->CHM_Option[OptionCount], &GetInt);
  351.       if (GetInt)
  352.          {
  353.          sprintf (TempBuffer, " %s", Pointers->ParsedSettings->Option[OptionCount]);
  354.          strcat (Command, TempBuffer);
  355.          }
  356.       }
  357.  
  358.    GetAttr (MUIA_String_Contents, Pointers->MUIPointers->STR_OutFile, &GetInt);
  359.    sprintf (TempBuffer, " %s%s ", Pointers->ParsedSettings->Device, GetInt);
  360.    strcat (Command, TempBuffer);
  361.    strcat (Command, Pointers->ParsedSettings->DVIFile);
  362.    strcat (Command, DVIFile);
  363.  
  364.    if (TmpFp = fopen (TempFileName, "w"))
  365.       {
  366.       fprintf (TmpFp, "failat 999\n");
  367.       fprintf (TmpFp, "stack 50000\n");
  368.       if (strcmp (Pointers->ParsedSettings->Copies, "  ")) Copies = 1;
  369.       for (Cnt = 0; Cnt < Copies; Cnt++) fprintf (TmpFp, "%s\n", Command);
  370.       fprintf (TmpFp, "Wait 5\n");
  371.       fprintf (TmpFp, "endcli\n");
  372.       fclose (TmpFp);
  373.       Delay (10);
  374.       stpcpy (Console, Args.ArgStr[KW_SHELL]);
  375.       strcat (Console, " from ");
  376.       strcat (Console, TempFileName);
  377.       system (Console);
  378.       }
  379.    }
  380.  
  381. set (Pointers->MUIPointers->app, MUIA_Application_Sleep, FALSE);
  382. }
  383.  
  384.  
  385.  
  386. /***************************************************************************
  387.  *                                                                         *
  388.  *  Function name : OpenLibs                                               *
  389.  *                                                                         *
  390.  *  Description : Opens the following libraries:                           *
  391.  *                 muimaster.library                                       *
  392.  *                                                                         *
  393.  ***************************************************************************
  394.  *                                                                         *
  395.  *  Synopsis : OpenLibs ()                                                 *
  396.  *                                                                         *
  397.  *  Parameters : None                                                      *
  398.  *                                                                         *
  399.  *  Return value : (int)                                                   *
  400.  *       0, if opening of all libraries succeeded.                         *
  401.  *      >0, if a library couldn't be openend.                              *
  402.  *                                                                         *
  403.  ***************************************************************************/
  404.  
  405. int OpenLibs (void);
  406.  
  407. int OpenLibs ()
  408.  
  409. {
  410. int Error = 0;
  411.  
  412. if (!(MUIMasterBase = OpenLibrary((UBYTE *)MUIMASTER_NAME, (ULONG)MUIMASTER_VMIN)))
  413.    Error++;
  414.  
  415. return (Error);
  416. }
  417.  
  418.  
  419.  
  420.  
  421. /***************************************************************************
  422.  *                                                                         *
  423.  *  Function name : Cleanup                                                *
  424.  *                                                                         *
  425.  *  Description : Frees all allocated memory, closes all used libraries,   *
  426.  *                closes all screens and windows.                          *
  427.  *                                                                         *
  428.  *                                                                         *
  429.  ***************************************************************************
  430.  *                                                                         *
  431.  *  Synopsis : Cleanup ()                                                  *
  432.  *                                                                         *
  433.  *  Parameters : None                                                      *
  434.  *                                                                         *
  435.  *  Return value : None                                                    *
  436.  *                                                                         *
  437.  ***************************************************************************
  438.  *                                                                         *
  439.  * Copyright © 1992 Richard Bödi,  All rights reserved.                    *                                                     *
  440.  *                                                                         *
  441.  ***************************************************************************/
  442.  
  443. void Cleanup ()
  444.  
  445. {
  446. DeleteFile (TempFileName);
  447.  
  448. if (AppIconObj)      FreeDiskObject (AppIconObj);
  449. if (AppIcon)         RemoveAppIcon (AppIcon);
  450. if (AppMsgPort)      DeleteMsgPort (AppMsgPort);
  451. if (MUIPointers.app) MUI_DisposeObject (MUIPointers.app);
  452. if (Menu)            free (Menu);
  453. if (MUIMasterBase)     CloseLibrary (MUIMasterBase);
  454. }
  455.  
  456. /*---------------------------- END OF FILE --------------------------------*/
  457.